/**
 * jims
 */
package com.jims.master.modules.finance.service;

import java.util.*;

import com.jims.master.common.utils.PrintPdfUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.doctor.dao.PatMasterIndexDao;
import com.jims.master.modules.doctor.dao.PatVisitDao;
import com.jims.master.modules.doctor.dao.PatVisitIndexDao;
import com.jims.master.modules.doctor.entity.PatMasterIndex;
import com.jims.master.modules.doctor.entity.PatVisit;
import com.jims.master.modules.doctor.entity.PatVisitIndex;
import com.jims.master.modules.sys.dao.CompanyDao;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.DeptUtils;
import com.jims.master.modules.sys.utils.DictUtils;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.finance.entity.InpBillDetail;
import com.jims.master.modules.finance.dao.InpBillDetailDao;

/**
 * Service
 * @author 赵宁
 * @version 2017-03-28
 */
@Service
@Transactional(readOnly = true)
public class InpBillDetailService extends CrudService<InpBillDetailDao, InpBillDetail> {

    @Autowired
    private InpBillDetailDao inpBillDetailDao;
    @Autowired
	private PatVisitDao patVisitDao;
    @Autowired
	private CompanyDao companyDao;
    @Autowired
    private PatVisitIndexDao patVisitIndexDao;



 	public InpBillDetail get(String id) {
		return super.get(id);
	}
	
	public List<InpBillDetail> findList(InpBillDetail inpBillDetail) {
		return super.findList(inpBillDetail);
	}
	
	public Page<InpBillDetail> findPage(Page<InpBillDetail> page, InpBillDetail inpBillDetail) {
		return super.findPage(page, inpBillDetail);
	}
	
	@Transactional(readOnly = false)
	public int save(InpBillDetail inpBillDetail) {
		return super.save(inpBillDetail);
	}
	
	@Transactional(readOnly = false)
	public int delete(InpBillDetail inpBillDetail) {
		return super.delete(inpBillDetail);
	}

    /**
     * 根据时间查询病区工作量
     * @param inpBillDetail
     * @return
     * @author 杨富达
	 */
	public Page<InpBillDetail> wardCodeFindPage(Page<InpBillDetail> page, InpBillDetail inpBillDetail) {
        inpBillDetail.setWardCode(UserUtils.getUser().getOrgDept() != null ? UserUtils.getUser().getOrgDept().getId() : "");
		inpBillDetail.updateOrgId();
        List<InpBillDetail> list=dao.wardCodeFindPage(inpBillDetail);
        page.setList(list);
		page.setCount(list.size());
        return page;
	}
    /**
     * 查询 病人费用明细
     * @param inpBillDetail
     * @return
     * @author 赵宁
     */
    public List<InpBillDetail> fingBillDetails(InpBillDetail inpBillDetail){
        return dao.findBillDetails(inpBillDetail);
    }

	/**
	 * 一日清单查询费用明细
	 * @param visitId
	 * @param startDate
	 * @param endDate
	 * @return
	 * @author 赵宁
	 */
	public List<BaseDto> findDetails(String visitId,String startDate,String endDate){
      return dao.findDetails(visitId,startDate,endDate);
	}

    /**
     * 护士段费用操作
     * @param inpBillDetail
     * @return
     */
    @Transactional(readOnly = false)
    public int nurseOperFee(InpBillDetail inpBillDetail) {
        inpBillDetail.setCosts(Integer.parseInt(inpBillDetail.getAmount())*inpBillDetail.getItemPrice());
        inpBillDetail.setCharges(inpBillDetail.getCosts());
        int resultVal=0;
        inpBillDetail.setSourceType("7");//费用补录
        String type=inpBillDetail.getType();
        if("1".equals(type)){//0 退费
            inpBillDetail.setAmount((-Integer.parseInt(inpBillDetail.getAmount()))+"");
            inpBillDetail.setCosts(-inpBillDetail.getCosts());
            inpBillDetail.setCharges(inpBillDetail.getCosts());
            if("A".equals(inpBillDetail.getItemClass())){
                inpBillDetail.setDelFlag("1");
                inpBillDetail.setRemarks("a");
            }
        }
        resultVal+=save(inpBillDetail);
        return resultVal;
    }

    /**
     * 确认退药
     * @param inpBillDetail
     * @return
     */
    @Transactional(readOnly = false)
    public int doConfirm(InpBillDetail inpBillDetail) {
        int resultVal=0;
        String[] idsArr=inpBillDetail.getIds().split(",");
        List<String> ids=Arrays.asList(idsArr);
        resultVal=inpBillDetailDao.doConfirm(ids);
        return resultVal;
    }

	/**
	 * 护士端-查询费用信息-打印
	 * @param inpBillDetail
	 * @return
	 * @author CTQ
	 */
	public String searchBillPrint(InpBillDetail inpBillDetail){
    	String printPath = "";
		User user = UserUtils.getUser();
		Map map = new HashMap();
		if (inpBillDetail != null) {
			if(StringUtils.isNotBlank(inpBillDetail.getVisitId())){
				PatVisit patVisit = patVisitDao.get(inpBillDetail.getVisitId());
				PatVisitIndex patMasterIndex = patVisit.getPatVisitIndex();
				map.put("hospital", companyDao.get(user.getOrgId()).getOrgName());
				map.put("inpNo",patVisit.getVisitNo());
				map.put("name", patMasterIndex.getName());
				map.put("chargeType", DictUtils.getDictValue(patMasterIndex.getChargeType(),"CHARGE_TYPE_DICT",""));
				map.put("deptName", DeptUtils.getDeptName(user.getOrgDept()!=null?user.getOrgDept().getId():"",""));
				if("undefined".equals(inpBillDetail.getPrepayments())){
					map.put("prepayments","0");
				}else{
					map.put("prepayments",inpBillDetail.getPrepayments());
				}
			}
		}

		List<BaseDto> baseDtoList = dao.findChargeNurse(inpBillDetail);
		if(baseDtoList !=null &&baseDtoList.size()>0){
			map.put("lists",baseDtoList);
			printPath = PrintPdfUtils.previewPdf(user.getId(), "his/nurse/chargeNurse.jasper", map, baseDtoList, "chargeNurse");
		}else{
			printPath="暂无数据";
		}
    	return printPath;
	}

	public String denomSignPrint(InpBillDetail inpBillDetail,String name){
		String printPath = "";
		User user = UserUtils.getUser();
		Map map = new HashMap();
		map.put("hospital", companyDao.get(user.getOrgId()).getOrgName());
		map.put("name", name);
		map.put("deptName", DeptUtils.getDeptName(user.getOrgDept()!=null?user.getOrgDept().getId():"",""));
		List<BaseDto> baseDtoList = dao.getNursePrice(inpBillDetail);
		printPath = PrintPdfUtils.previewPdf(user.getId(), "nurse/inpBillNurse.jasper", map, baseDtoList, "inpBillNurse");
		return printPath;
	}

    /**
     * 根据月份查询收费数据
     * @param orgId 机构id
     * @param startDate 开始时间
     * @param  endDate 结束时间
     * @return
     */
    public List<BaseDto> queryMonthList(String orgId,Date startDate,Date endDate) {
        return dao.queryMonthList(orgId,startDate,endDate);
    }



    /**
     * 费用分组查询
     * @param inpBillDetail
     * @return
     * @author zhangyao
     * @date 2017/7/20
     */
    public List<InpBillDetail> findListDrugsGroup(InpBillDetail inpBillDetail) {
        return dao.findListDrugsGroup(inpBillDetail);
    }



    /**
     * 多条件查询 病人住院费用明细
     * 医保住院上传
     * @param inpBillDetail
     * @return
     * @author xueliang
     */
    public Page<InpBillDetail> findListBy(Page<InpBillDetail> page, InpBillDetail inpBillDetail) {
        inpBillDetail.updateOrgId();
        inpBillDetail.setPage(page);
        page.setList(dao.findListBy(inpBillDetail));
        return page;
    }

    /**
     * 医嘱瓶签list
     * @param prescriptionApply
     * @return
     * @author lizhongyu
     */
    public Page<InpBillDetail> findByClinicPage(Page<InpBillDetail> page, InpBillDetail inpBillDetail) {
        inpBillDetail.updateOrgId();
        inpBillDetail.setPage(page);
        inpBillDetail.setOrderDept(null != UserUtils.getUser().getOrgDept() ? UserUtils.getUser().getOrgDept().getId() : "");
        page.setList(dao.findByInpPage(inpBillDetail));
        return page;
    }
    /**
     * 住院医保--病人费用详情信息
     * 查询医保登记病人费用全部信息
     * where patVisit.id patVisit.patientId patVisit.orgId
     * @return
     */
    public Page<InpBillDetail> getMedInsInpBillDetail(Page<InpBillDetail> page, InpBillDetail inpBillDetail) {
        PatVisit patVisit = new PatVisit();
        patVisit.setId(inpBillDetail.getPatVisit().getId());
        patVisit.setVisitNo(inpBillDetail.getPatVisit().getVisitNo());
        patVisit.setPatientId(inpBillDetail.getPatVisit().getPatientId());
        patVisit.setOrgId(UserUtils.getOrgId());
        inpBillDetail.setPatVisit(patVisit);
        List<BaseDto> baseDtoList = inpBillDetailDao.findBillDetailList(inpBillDetail);
        List<InpBillDetail> inpBillDetailList= new ArrayList<>();
        PatVisitIndex patVisitIndex = patVisitIndexDao.getInpPateientinfo(inpBillDetail.getPatVisit().getId());
        for (BaseDto baseDto : baseDtoList){
            InpBillDetail InpBillDetail = inpBillDetailDao.get(String.valueOf(baseDto.get("id")));
            patVisit.setPatVisitIndex(patVisitIndex);
            InpBillDetail.setPatVisit(patVisit);
            inpBillDetailList.add(InpBillDetail);
        }
        page.setList(inpBillDetailList);
        return page;
    }

    /**
     * emr接口查询病案首页费用信息
     * @param visitId
     * @param patientId
     * @return
     * @author 赵宁
     */
      public List<BaseDto> findMrCosts(String visitId,String patientId){
        return dao.findMrCosts(patientId,visitId);
      }
}